<?php
include_once('../lib/Sqlexecutor.php');
include_once('../lib/Login.php');
include_once('../lib/Series.php');
include_once('../lib/Tile.php');
include_once('../lib/TileDiagram.php');
include_once('../lib/TileUserInfo.php');
include('log4php/Logger.php');

Logger::configure('config.ini');
$logger = Logger::getLogger('intf');

$usrid = isset($_REQUEST['usrid']) ? $_REQUEST['usrid'] : 0;
$sessionid = isset($_REQUEST['sessionid']) ? $_REQUEST['sessionid'] : '';
$series_id = isset($_REQUEST['series_id']) ? $_REQUEST['series_id'] : '';
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 1;
/*
if($series_id == '')
{
    $res['errno'] = -2;
    $output = json_encode($res);
    $logger->trace("input: series_id=$series_id,type=$type; res: $output");
    echo $output;
    exit;
}
 */

$dbh = new Sqlexecutor('localhost', 'tile', 'tile', 'tile');
$is_login = false;
$l = new Login($dbh, $logger);
$info = $l->isLogin($usrid, $sessionid);
if($info === "1")
{
    $is_login = true; 
}

if($type == 1)
{
    $s = new Tile($dbh, $logger);
    if($series_id == '')
        $list = $s->getAllList(); 
    else
        $list = $s->getListBySeriesID($series_id);

    if($list === false)
    {
        $res['errno'] = -100;
    }
    elseif($is_login == false)
    {
        foreach($list as $k => $value)
        {
            $value['sublist'] = array();
            $list[$k] = $value;
        }
        $res['tiles'] = $list;
        $res['errno'] = 0;
    }
    else
    {
        $u = new TileUserInfo($dbh, $logger);

        if($series_id == '')
            $price_score_list = $u->getAllList($usrid);
        else
            $price_score_list = $u->getListBySeriesID($usrid, $series_id);

        foreach($price_score_list as $info)
        {
            $tile_map[$info['tile_id']] = $info;
        }
        foreach($list as $k => $value)
        {
            $id = $value['id'];
            if(isset($tile_map[$id]))
            {
                $value['price'] = $tile_map[$id]['price'];
                $value['score'] = $tile_map[$id]['score'];
            }
            $value['sublist'] = array();
            $list[$k] = $value;
        }
        $res['tiles'] = $list;
        $res['errno'] = 0;
    }
}
elseif($type == 2)
{
    $s = new TileDiagram($dbh, $logger);
    if($series_id == '')
        $list = $s->getAllList();
    else
        $list = $s->getListBySeriesID($series_id);
    if($list === false)
    {
        $res['errno'] = -100;
    }
    else
    {
        $s = new Tile($dbh, $logger);
        foreach($list as $k => $value)
        {
            $ids = $value['tile_ids'];
            $ids = trim($ids, ",");
            if($ids == '')
            {
                $value['sublist'] = array();
            }
            else
            {
                $value['sublist'] = $s->getInfoInIDS($ids);
            }
            $list[$k] = $value;

        }
        $res['tiles'] = $list;
        $res['errno'] = 0;
    }
}
else
{
    $res['errno'] = -2;
}

$output = json_encode($res);
$logger->trace("input: series_id=$series_id,type=$type; res: $output");
echo $output;
